using System;
using Org.BouncyCastle.Math;
namespace ch.ethz.ssh2.crypto.digest
{
	
	/// <summary> HashForSSH2Types.
	/// 
	/// </summary>
	/// <author>  Christian Plattner
	/// </author>
	/// <version>  2.50, 03/15/10
	/// </version>
	public class HashForSSH2Types
	{
		virtual public int DigestLength
		{
			get
			{
				return md.DigestLength;
			}
			
		}
		internal Digest md;
		
		public HashForSSH2Types(Digest md)
		{
			this.md = md;
		}
		
		public HashForSSH2Types(System.String type)
		{
			if (type.Equals("SHA1"))
			{
				md = new SHA1();
			}
			else if (type.Equals("MD5"))
			{
				md = new MD5();
			}
			else
				throw new System.ArgumentException("Unknown algorithm " + type);
		}
		
		public virtual void  updateByte(byte b)
		{
			/* HACK - to test it with J2ME */
			byte[] tmp = new byte[1];
			tmp[0] = b;
			md.update(tmp);
		}
		
		public virtual void  updateBytes(byte[] b)
		{
			md.update(b);
		}
		
		public virtual void  updateUINT32(int v)
		{
			md.update((byte) (v >> 24));
			md.update((byte) (v >> 16));
			md.update((byte) (v >> 8));
			md.update((byte) (v));
		}
		
		public virtual void  updateByteString(byte[] b)
		{
			updateUINT32(b.Length);
			updateBytes(b);
		}

        public virtual void updateBigInt(BigInteger b)
        {
            updateByteString(b.ToByteArray());
        }
		
		public virtual void  reset()
		{
			md.reset();
		}
		
		public virtual byte[] getDigest()
		{
			byte[] tmp = new byte[md.DigestLength];
			getDigest(tmp);
			return tmp;
		}
		
		public virtual void  getDigest(byte[] output)
		{
			getDigest(output, 0);
		}
		
		public virtual void  getDigest(byte[] output, int off)
		{
			md.digest(output, off);
		}
	}
}